<?php
declare (strict_types = 1);

namespace app\middleware;

class AddHeaders
{
    public function handle($request, \Closure $next)
    {
        //允许跨域来源
        $acceptHosts = ['http://localhost:8080','http://localhost:8081','https://schub.top','https://www.schub.top','https://www.suancaixianyu.cn','https://suancaixianyu.cn','https://schub.schz.top','https://schubm.schz.top'];
        $response = $next($request);
        if(array_key_exists('HTTP_ORIGIN',$_SERVER)){
            $origin = $_SERVER['HTTP_ORIGIN'];
            foreach ($acceptHosts as $v){
                if(strpos($origin,$v)!=-1){
                    $headers = [];
                    $headers['Access-Control-Allow-Origin'] = $origin;
                    $headers['Access-Control-Allow-Headers'] = 'Content-Type,Access-Control-Allow-Origin,Authorization';
                    $headers['Access-Control-Allow-Credentials'] = 'true';
                    $response->header($headers);
                    break;
                }
            }
        }
        return $response;
    }
}